<template>
  <div>
 <h1>优惠卷增加</h1>
    <table class="table table-bordered">
      <tbody>
      <tr>
        <td>优惠卷类型</td>
        <td>
          <select v-model="info.DisCountType">
            <option value="">请选择</option>
            <option :value="1">代金券</option>
            <option :value="2">折扣券</option>
            <option :value="3">兑换券</option>
          </select>
        </td>
      </tr>
     <tr>
       <td>优惠卷标题</td>
       <td><input type="text" v-model="info.Titel"/></td>
     </tr>
      <tr>
        <td>优惠卷副标题</td>
        <td><input type="text"v-model="info.SubTitel" /></td>
      </tr>
      <tr>
        <td>优惠卷图片</td>
        <td><input type="file" @change="img"/>
          <img :src="info.Photo" width="100" height="100" v-if="info.Photo!='' "/>
        </td>
      </tr>
      <tr>
        <td>库存</td>
        <td><input type="text" v-model="info.Inventory"/></td>
      </tr>
      <tr>
        <td>每人限领</td>
        <td><input type="text" v-model="info.PeopleMaxNum" disabled/></td>
      </tr>
      <tr>
        <td>使用条件</td>
        <td><input type="text" v-model="info.Money"/></td>
      </tr>

      <tr  v-if="info.DisCountType==1">
        <td>减免金额</td>
        <td><input type="text" v-model="info.SubMoney"/></td>
      </tr>
<!---->
      <tr v-if="info.DisCountType==2">
        <td>折扣价</td>
        <td><input type="text" v-model="info.ZhekoMoney"/></td>
      </tr>
<!---->
      <tr>
        <td>有效期</td>
        <td><input type="date" v-model="info.BeginTime"/>-<input type="date" v-model="info.EndTime"/></td>
      </tr>
      <tr>
        <td>适用业务</td>
        <td>
          <input type="checkbox" name="serices" value="订房" v-model="info.Serices"/>订房
          <input type="checkbox" name="serices" value="商城"  v-model="info.Serices"/>商城
          <input type="checkbox" name="serices" value="堂食"  v-model="info.Serices"/>堂食
          <input type="checkbox" name="serices" value="付款码"  v-model="info.Serices"/>付款码
        </td>
      </tr>
      <tr>
        <td>使用须知</td>
        <td>
          <textarea rows="10" cols="50" v-model="info.Notice">

        </textarea></td>
      </tr>
      <tr>
        <td>适用门店</td>
        <td>
          <select  v-model="info.ShopId">
            <option value="">请选择</option>
            <option :value="item.id" v-for="item in data.list">{{item.ShopName}}</option>
          </select>

        </td>
      </tr>
      <tr>
        <td>服务电话</td>
        <td><input type="text" v-model="info.Phone" maxlength="11"/></td>
      </tr>
      <tr>
        <td>颜色</td>
        <td><input type="color" v-model="info.Color"/></td>
      </tr>
      <tr>
        <td>是否发布</td>
        <td>
          <input type="radio" name="state" v-model="info.State" :value="1"/>是
          <input type="radio" name="state" v-model="info.State" :value="0"/>否
        </td>
      </tr>
      <tr>
        <td></td>
        <td><input type="button" value="确定" class="btn btn-success" @click="Ok"/></td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import moment from "moment";
const router = useRouter()
const route = useRoute()

onMounted(() => {
  getShopList();
})
//图片上传
const img=(e:any)=>{
  let obj=e.target.files[0];
  let f=new FormData();
  f.append("file",obj);
  //发送问了请求
  axios({
      url:'/api/Img/Img',
      method:'post',
      data:f
  }).then((res)=>{
    console.log(res)
    if(res.data=="不是图片"){
      alert("您上传的不是图片")
      return;
    }
    if(res.data=="大于2M")
    {
      alert("您上传的图片大于2M")
      return;
    }
    info.Photo=res.data;
  })
}
//门店列表

//增加操作

let info:any=reactive({
  "DisCountType": "",
  "Titel": "",
  "SubTitel": "",
  "Photo": "",
  "Inventory": '',
  "PeopleMaxNum": 1,
  "Money": '',
  "BeginTime": "",
  "EndTime": "",
  "Serices": [],
  "Notice": "",
  "ShopId": '',
  "Phone": "",
  "Color": "#ff0000",
  "State": 1,
  "SubMoney": 0,
  "ZhekoMoney": 10
})

let info1:any=reactive({
  pageIndex:1,
  pageSize:100,
  shopName:"",
  address:""
});
let data:any =reactive({
  list:[],
  totalCount:0,
  totalPage:0,
})
const  getShopList = () => {
  axios({
    url:'/api/Shop/GetShopList',
    params:info1,
    method:'get'

  }).then((res) => {
    console.log(res)
    data.list=res.data.list
    data.totalCount=res.data.totalCount
    data.totalPage=res.data.totalPage
    //Object.assign(data,res.data);

  })
}
const  Ok=()=>{
  //有效判断（使用条件 减免金额 折扣价)
  if(info.Money=="")
  {
    alert("使用条件不能为空")
    return;
  }
  let reg = /^[0-9]\d*$/;
  if(!reg.test(info.Money)){
    alert("使用条件必须大于0的数")
    return;
  }
  if(info.SubMoney=="" && info.DisCountType==1){
    alert("减免金额不能为空")
    return;
  }
  if(reg.test(info.SubMoney&& info.DisCountType==1 )){
    alert("减免金额只能是正整数")
    return;
  }

  if(info.ZhekoMoney==="" && info.DisCountType==2){
    alert("折扣金额不能为空")
    return;
  }
  if(isNaN((info.ZhekoMoney)&& info.DisCountType==2))
  {
    alert("折扣金额只能是正整数")
    return;
  }
  if((info.Zheko>9.9|| info.Zheko<1) && info.DisCountType==2){
    alert("只能在于9.9和1之间")
  }
      info.Serices=info.Serices.toString();
  //发送网络请求
  axios({
    url:'/api/DisCount/DisCountAdd',
    method:'post',
    data:info,
  }).then((res) => {
    console.log(res)
    if(res.data==-1){
      alert("优惠券重复")
      return;
    }
    if(res.data>0){
      alert("添加成功")
      return;
    }
    else {
      alert("添加失败")
      return;
    }
  })

}
</script>

<style scoped>

</style>